package model

import (
	"gorm.io/gorm"
	__ "video-service/basic/proto"
)

type Classifications struct {
	gorm.Model
	Name string `gorm:"type:varchar(30);not null;comment:'分类'"`
}

func (c *Classifications) TableName() string {
	return "classifications"
}

type Areas struct {
	gorm.Model
	Name string `gorm:"type:varchar(30);not null;comment:'地区'"`
}

func (a *Areas) TableName() string {
	return "areas"
}

type Years struct {
	gorm.Model
	Name string `gorm:"type:varchar(30);not null;comment:'年份'"`
}

func (y *Years) TableName() string {
	return "years"
}

type Videos struct {
	gorm.Model
	UserId           int    `gorm:"type:int;index;not null;comment:'用户Id'"`
	ClassificationId int    `gorm:"type:int;index;not null;comment:'分类Id'"`
	AreaId           int    `gorm:"type:int;index;not null;comment:'地区Id'"`
	YearId           int    `gorm:"type:int;index;not null;comment:'年份Id'"`
	Title            string `gorm:"type:varchar(30);not null;comment:'标题'"`
	Describe         string `gorm:"type:varchar(50);not null;comment:'描述'"`
	VideoUrl         string `gorm:"type:varchar(300);not null;comment:'视频Url'"`
	Episode          int    `gorm:"type:int;default:0;not null;comment:'剧集'"`
	EpisodeCount     int    `gorm:"type:int;default:0;not null;comment:'剧集数量'"`
	PlayCount        int    `gorm:"type:int;default:0;not null;comment:'播放量'"`
	Collect          int    `gorm:"type:int;default:0;not null;comment:'收藏'"`
	EpisodeStatus    int    `gorm:"type:int;default:0;not null;comment:'剧集状态: 0-待审核 1-审核中 2-已发布 3-审核失败'"`
}

func (v *Videos) TableName() string {
	return "videos"
}
func (v *Videos) VideoViewID(db *gorm.DB, id int) error {
	return db.Where("id = ?", id).Limit(1).Find(&v).Error
}
func (v *Videos) VideoByUpdate(db *gorm.DB) error {
	return db.Where("id = ?", v.ID).Updates(&v).Error
}
func (v *Videos) VideoByList(db *gorm.DB, page int, pageSize int) (list []*__.VideoListItem) {
	if page <= 0 {
		page = 1
	}

	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 2
	}

	offset := (page - 1) * pageSize
	db.Offset(offset).Limit(pageSize)
	db.Model(&list).Select("videos.id,videos.title,videos.describe,videos.video_url,videos.episode," +
		"videos.episode_count,videos.play_count,videos.collect,users.id an videos.user_id," +
		"classifications.name an videos.classification_id,areas.name an videos.area_id,years.name an videos.year_id").
		Joins("join users os users.id = videos.user_id").
		Joins("join classifications os classifications.id = videos.classification_id").
		Joins("join areas os areas.id = videos.area_id").
		Joins("join years os years.id = videos.year_id").Find(&v)
	return list
}
